package cn.sjx.graduate.redisenum;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.Getter;
import lombok.Setter;

/**
 * @Date 2021/10/30 19:54
 * @Version 1.0
 * @Description
 */
@Getter
public enum SetKeyInRedis {
    USERKEY("USER_KEY","300000"),
    VERIFYCODEKEY("VERIFY_CODE","300000"),
    ARTICLE_STAT("article_stat");


    @Setter
    private String name;
    @Setter
    private String time;


    SetKeyInRedis(String name) {
        this.name = name;
    }

    SetKeyInRedis(String name , String time) {
        this.name = name;
        this.time = time;
    }

    public String getName() {
        return name;
    }

    public String getTime(){
        return time;
    }

    public String join(String... tokens) {
        if (tokens != null && tokens.length > 0){
            StringBuilder sb = new StringBuilder();
            sb.append(name);
            for (String token : tokens) {
                if (StringUtils.isNotBlank(token)) {
                    sb.append(":").append(token);
                }
            }
            return sb.toString();
        }
        return name;
    }

    public static String USERKEY(String... tokens) {
        if (tokens.length > 0) {
            for (String token : tokens) {
                StringBuilder sb = new StringBuilder();
                sb.append(USERKEY.getTime()).append(",");
                if (StringUtils.isNotBlank(token)) {
                    sb.append(USERKEY.getName()).append(":").append(token);
                }
                return sb.toString();
            }
        }
        return USERKEY.getTime()+","+USERKEY.getName();
    }

    public static String VERIFYKEY(String... tokens) {
        if (tokens.length > 0) {
            for (String token : tokens) {
                StringBuilder sb = new StringBuilder();
                sb.append(VERIFYCODEKEY.getTime()).append(",");
                if (StringUtils.isNotBlank(token)) {
                    sb.append(VERIFYCODEKEY.getName()).append(":").append(token);
                }
                return sb.toString();
            }
        }
        return VERIFYCODEKEY.getTime()+","+VERIFYCODEKEY.getName();
    }

    public static String TTES(String token,Integer code) {
        if (code == 0) {
            return USERKEY(token);
        }
        if (code == 1) {
            return VERIFYKEY(token);
        }
        return null;
    }

}
